Ext.ns('ru.proburo.oms.order');

/**
 * ru.proburo.oms.order.Activator
 * @extends ru.proburo.oms.module.AbstractActivator
 */
ru.proburo.oms.order.Activator = Ext.extend(ru.proburo.oms.module.AbstractActivator, {
    
	createdOrders: 0,
	
	start: function(viewport)
    {        
        var tree = this._initTree(viewport.layout.west.panel);
        
        this.panel = viewport.layout.center.panel;
        
        this._initTab(tree);
        
        this._initActions();
    },
    
    // private
    _initTree: function(panel)
    {
        var tree = panel.add(new ru.proburo.oms.order.components.Tree({
            title:   'Заказы',
            iconCls: 'silk-table'
        }));
        
        panel.doLayout();
        
        return tree;
    },
    
    // private
    _initTab: function(tree)
    {        
        var tab = this.panel.add(new ru.proburo.oms.order.components.List({
            title:       'Заказы',
            iconCls:     'silk-table',
            border:      true,
            header:      false,
            forceLayout: true,
            
            tree:      tree,
            activator: this
        }));		
		this.panel.hideTabStripItem(tab);
		this.panel.orderTab = tab;
        
        tree.on('expand', function(p){
            var key = this.panel.items.getKey(tab);
            this.panel.hideTabStripItem(this.panel.currentFTab);
            this.panel.setActiveTab(key);
            this.panel.unhideTabStripItem(tab);
            this.panel.currentFTab = tab;
        }, this);
        
        tab.on({
            rowdblclick: this._onRowDblClick,
            scope:       this
        });
    },
    
    _initActions: function()
    {
        var grid = this.panel.catalogTab.items.get(0);
        
        this.addAction1 = new ru.proburo.oms.order.SplitButton({
			collection:    this.panel.items,
            disabled:      true,
            handler:       this._onAddClick,
            scope:         this
		});
        this.addAction2 = new ru.proburo.oms.order.SplitButton({
			collection:    this.panel.items,
            disabled:      true,
            handler:       this._onAddClick,
            scope:         this
		});
        
        grid.getSelectionModel().on('selectionchange', function(sm){
            this.addAction1.setDisabled(!sm.getCount());
            this.addAction2.setDisabled(!sm.getCount());
        }, this);
        
        // Добавляем контрол в тулбар каталога
        var tbar = grid.getTopToolbar();
        tbar.insert(tbar.items.getCount()-1, 'Добавить в:');
        tbar.insert(tbar.items.getCount()-1, this.addAction1);
        tbar.insert(tbar.items.getCount()-1, '-');
        
        // Добавляем контрол в тулбар подробного описания
        var bbar = this.panel.catalogTab.items.get(1).getBottomToolbar();
        bbar.add('Добавить в:');
        bbar.add(this.addAction2);
    },
    
    _onRowDblClick: function(grid, rowIndex, event)
    {
        var record = grid.getSelectionModel().getSelected();
        
        if(record)
        {
            this._openOrder(record);
        }
    },
    
    _onAddClick: function(tab)
    {
        var sm = this.panel.catalogTab.items.get(0).getSelectionModel();
        
		tab.addRecords(sm.getSelections());
		
		sm.clearSelections();
    },
    
    _createOrder: function(data)
    {
		data = data || {};
		
		Ext.applyIf(data, {
			contact_person: '',
            phone: 			'',
            delivery_date:  new Date(),
            status: 		0
		});
		
        var record = new ru.proburo.oms.order.entities.Order(data);
        
        this.panel.orderTab.store.add(record);
		
		this.createdOrders++;
        
        return record;
    },
	
	_createFuckinOrder: function(data, linesData)
	{
		console.log('addRecordsRaw');
		
		var tab = this._openOrder(this._createOrder(data));
		
		tab.grid.addRecords(linesData);
	},
    
    _openOrder: function(record)
    {
        if(record)
        {
            var key = this.panel.items.findIndexBy(function(tab){
                return tab.record && tab.record.id === record.id;
            });
            
            if(-1 === key)
            {
                var newTab = this.panel.add(new ru.proburo.oms.order.components.Detail({
                    record:    record,
					activator: this
                }));
                
                key = this.panel.items.getKey(newTab);
            }
            
            this.panel.setActiveTab(key);
            
            return newTab;
        }
    }
});
ru.proburo.oms.Bootstrap.reg(ru.proburo.oms.order.Activator);